<template>
  <BasicModal
    @register="register"
    title="配置驱动"
    width="800px"
    :helpMessage="['品牌商设备型号驱动信息']"
    :okButtonProps="{ disabled: false }"
    @ok="handleSubmit"
  >
    <h3>设备型号: {{ modelname }}</h3>
    <Drive />
    <Row class="drive-foot-row">
      <Col span="8"> </Col>
      <Col span="16">
        <Button type="link" class="drive-chose-btn" @click="download">
          <template #icon><Icon icon="ant-design:download-outlined" /></template>下载示例
        </Button>
      </Col>
    </Row>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { Row, Col, Button, Upload } from 'ant-design-vue';
  import { Icon } from '/@/components/Icon/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import Drive from '../../drive/index.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import Bus from '/@/utils/helper/eventbus';
  import { Setdefaultdrive } from '/@/api/sys/gateway/gateway';
  export default defineComponent({
    components: { BasicModal, Row, Col, Drive, Button, Icon, Upload },
    setup() {
      const { createMessage } = useMessage();
      const [register, { redoModalHeight, closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      let modelname = ref();
      let device_type_id = ref();
      let drive_id = ref();
      function onDataReceive(data) {
        modelname.value = data.rowdata.name;
        device_type_id.value = data.rowdata.id;
        Bus.$emit('send_device_type_id', data.rowdata);
      }
      //接受选中的驱动id
      Bus.$on('offdriveid', (data) => {
        if (data) {
          drive_id.value = data.id;
        }
      });

      //设置默认驱动
      async function handleSubmit() {
        let res = await Setdefaultdrive({
          device_type_id: device_type_id.value,
          drive_id: drive_id.value,
        });
        closeModal();
        createMessage.success('设置成功');
      }

      let URLAPI =
        'http://iot-dev.c9d1c973fa315470eb49b82764004d642.cn-hangzhou.alicontainer.com/api';
      //下载示例
      function download() {
        let apiUrl = URLAPI + '/drive/download_drive_example';
        try {
          var elemIF = document.createElement('iframe');
          elemIF.src = apiUrl;
          elemIF.style.display = 'none';
          document.body.appendChild(elemIF);
        } catch (e) {
          createMessage.error('下载异常');
        }
      }

      setTimeout(() => {
        redoModalHeight();
      }, 1500);
      return {
        register,
        modelname,
        device_type_id,
        download,
        handleSubmit,
      };
    },
  });
</script>

<style lang="less">
  .ant-upload-list {
    display: none;
  }
</style>
